import Vue from 'vue';
import App from './App.vue';
import router from './router';

//引入和配置axios
import axios from 'axios';
// 绑定到原型
Vue.prototype.$axios = axios;
//设置默认的 api 域名基准路径
axios.defaults.baseURL = 'http://127.0.0.1:3000';

// 引入 Element-ui 库
import ElementUI from 'element-ui';
// 引入 css
import 'element-ui/lib/theme-chalk/index.css';
//注册组件库
Vue.use(ElementUI);

// 监听
axios.interceptors.request.use((config) => {
	if (!config.headers.Authorization && localStorage.getItem('token')) {
		config.headers.Authorization = localStorage.getItem('token');
	}
	return config;
});

//导航守卫
router.beforeEach((to, from, next) => {
	if (to.path != '/login') {
		if (!localStorage.getItem('token')) {
			return router.push('/login');
		}
	}
	next();
});

Vue.config.productionTip = false;

new Vue({
	router,
	render: function(h) {
		return h(App);
	}
}).$mount('#app');
